﻿using PearTunes.WEB.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using PearTunes.Model;
using System.Collections.Generic;
using System.Linq;

namespace PearTunes.UnitTest
{
    
    
    /// <summary>
    ///This is a test class for MusicModelTest and is intended
    ///to contain all MusicModelTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MusicModelTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for DeleteTrack
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void DeleteTrackTest()
        {
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            context.Tracks.Add(new Track() { Title = "Model_DeleteTrackTest" });
            context.SaveChanges();
            MusicModel target = new MusicModel();
            int trackId = (new DAL.PearTunesEntities()).Tracks.Where(tr => tr.Title == "Model_DeleteTrackTest").First().TrackId;
            target.DeleteTrack(trackId);
            int expected = 0;
            int actual = (new DAL.PearTunesEntities()).Tracks.Where(tr => tr.Title == "Model_DeleteTrackTest").Count();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ListAlbums
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void ListAlbumsTest()
        {
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            int expected = 0;
            if (context.Albums.Count() > 0)
                expected = context.Albums.Count();
            else
            {
                context.Albums.Add(new Album() { Title = "ListAlbumsTest1" });
                context.Albums.Add(new Album() { Title = "ListAlbumsTest2" });
                context.Albums.Add(new Album() { Title = "ListAlbumsTest3" });
                context.SaveChanges();
                expected = 3;
            }
            int actual = (new MusicModel()).ListAlbums().Count();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ListTracks
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void ListTracksTest()
        {
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            int expected = 0;
            if (context.Tracks.Count() > 0)
                expected = context.Tracks.Count();
            else
            {
                context.Tracks.Add(new Track() { Title = "ListTracksTest1" });
                context.Tracks.Add(new Track() { Title = "ListTracksTest2" });
                context.Tracks.Add(new Track() { Title = "ListTracksTest3" });
                context.SaveChanges();
                expected = 3;
            }
            int actual = (new MusicModel()).ListTracks().Count();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for NewTrack
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void NewTrackTest()
        {
            MusicModel target = new MusicModel();
            List<Album> a = new List<Album>();
            a.Add(new Album() { Title = "NewTrackTest", Tracks = new List<Track>() });
            Track t = new Track()
            {
                Albums = a,
                Genre = "NewTrackTest",
                FilePath = "NewTrackTest",
                Price = 1,
                Title = "NewTrackTest",
                Artist = "NewTrackTest",
                TrackNumber = 1,
                Year = 2011
            };
            target.NewTrack(t);
            int expected = 1;
            int actual = (new DAL.PearTunesEntities()).Tracks.Where(tr => tr.Title == "NewTrackTest").Count();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for SearchForAlbum
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void SearchForAlbumTest()
        {
            Album toAdd = new Album() { AlbumId = 1, Title = "SearchForAlbumTest", Tracks = new List<Track>(), Price = 1, Year = 2011 };
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            context.Albums.Add(toAdd);
            context.SaveChanges();
            Assert.IsNotNull((new MusicModel()).SearchForAlbum("SearchForAlbumTest"));
        }
    }
}
